package com.dmap.run.service.impl;

import com.dmap.run.controller.api.param.RunRecordParam;
import com.dmap.run.dao.RunRecordMapper;
import com.dmap.run.po.RunRecord;
import com.dmap.run.po.RunRecordExample;
import com.dmap.run.service.RunRecordService;
import com.dmap.run.vo.RunRecordHistoryVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * \* User: silent
 * \* Date: 16/05/18 Time: 16:26
 * \* Description: 跑步记录模块
 * \
 */
@Service
public class RunRecordServiceImpl implements RunRecordService {
    @Resource
    private RunRecordMapper runRecordMapper;

    @Override
    public boolean insertRunRecord(RunRecordParam param) {
        RunRecord record = new RunRecord();
        record.setUserId(param.getUserId());
        record.setStartTime(param.getStartTime());
        record.setEndTime(param.getEndTime());
        record.setType(param.getType());
        record.setDataSource(param.getDataSource());
        record.setPlace(param.getPlace());
        //各类指标的目标值(里程,卡路里,时间)
        record.setDistanceGoal(param.getDistanceGoal());
        record.setCalorieGoal(param.getCalorieGoal());
        record.setTimeGoal(param.getTimeGoal());
        //各类指标的总消耗(累计爬升,步幅,卡路里消耗)
        record.setAltitude(param.getAltitude());
        record.setStride(param.getStride());
        record.setCalorie(param.getCalorie());
        //各类指标的总数(总步数,总计时间,全程距离）
        record.setDistanceTotal(param.getDistanceTotal());
        record.setTimeTotal(param.getTimeTotal());
        record.setStepNumTotal(param.getStepNumTotal());
        //各类指数的最高值,平均值,最低值(配速,步频,心率)
        record.setSpeedAverage(param.getSpeedAverage());
        record.setSpeedQuickest(param.getSpeedQuickest());
        record.setSpeedSlowest(param.getSpeedSlowest());
        record.setStrideFrequencyAverage(param.getStrideFrequencyAverage());
        record.setStrideFrequencyQuickest(param.getStrideFrequencyQuickest());
        record.setStrideFrequencySlowest(param.getStrideFrequencySlowest());
        record.setHeartRateAverage(param.getHeartRateAverage());
        record.setHeartRateQuickest(param.getHeartRateQuickest());
        record.setHeartRateSlowest(param.getHeartRateSlowest());
        record.setCreateTime(System.currentTimeMillis() / 1000);
        return runRecordMapper.insertSelective(record) == 1;
    }

    @Override
    public boolean isExistRunRecord(long userId, long startTime, long endTime) {
        RunRecordExample runRecordExample = new RunRecordExample();
        runRecordExample.createCriteria().andUserIdEqualTo(userId).andStartTimeEqualTo(startTime).andEndTimeEqualTo(endTime);
        return runRecordMapper.countByExample(runRecordExample) > 0;
    }

    @Override
    public RunRecordHistoryVo selectRunRecordHistoryList(long userId) {
        RunRecordHistoryVo runRecordHistoryVo = new RunRecordHistoryVo();
        RunRecordExample runRecordExample = new RunRecordExample();
        runRecordExample.createCriteria().andUserIdEqualTo(userId);
        int runNum = runRecordMapper.countByExample(runRecordExample);
        if (runNum == 0) {
            runRecordHistoryVo.setRunRecordSimpleList(Collections.emptyList());
            runRecordHistoryVo.setRunNum(0);
            return runRecordHistoryVo;
        }
        List<RunRecord> runRecordList = runRecordMapper.selectByExample(runRecordExample);
        int timeTotal = 0;
        int calorie = 0;
        int speedAverageTotal = 0;
        int distanceTotal = 0;
        for (RunRecord record : runRecordList) {
            timeTotal = timeTotal + record.getTimeTotal();
            //处理
            if (record.getDistanceTotal() > 0) {
                if ((record.getCalorie() / record.getDistanceTotal()) * 1000 > 150) {
                    calorie = calorie + record.getCalorie() / 1000;
                } else {
                    calorie = calorie + record.getCalorie();
                }
            }
            speedAverageTotal = speedAverageTotal + record.getSpeedAverage();
            distanceTotal = distanceTotal + record.getDistanceTotal();
        }
        //设置总数据
        runRecordHistoryVo.setTimeTotal(timeTotal);
        runRecordHistoryVo.setRunNum(runNum);
        runRecordHistoryVo.setCalorie(calorie);
        runRecordHistoryVo.setSpeedAverage(speedAverageTotal / runNum);
        runRecordHistoryVo.setDistanceTotal(distanceTotal);
        return runRecordHistoryVo;
    }

    public List<RunRecord> selectRunRecordListByPage(long userId, int pageIndex, int pageSize) {
        RunRecordExample runRecordExample = new RunRecordExample();
        runRecordExample.setLimitStart((pageIndex - 1) * pageSize);
        runRecordExample.setLimitEnd(pageSize);
        runRecordExample.setOrderByClause("start_time desc");
        runRecordExample.createCriteria().andUserIdEqualTo(userId);
        List<RunRecord> runRecordList = runRecordMapper.selectByExample(runRecordExample);
        return CollectionUtils.isNotEmpty(runRecordList) ? runRecordList : Collections.emptyList();
    }


    @Override
    public RunRecord selectRunRecordById(long runRecordId) {
        return runRecordMapper.selectByPrimaryKey(runRecordId);
    }

    @SuppressWarnings("Duplicates")
    @Override
    public int selectUserTotalDistance(long userId) {
        RunRecordExample runRecordExample = new RunRecordExample();
        runRecordExample.createCriteria().andUserIdEqualTo(userId);
        List<RunRecord> runRecordList = runRecordMapper.selectByExample(runRecordExample);
        if (CollectionUtils.isNotEmpty(runRecordList)) {
            int totalDistance = 0;
            for (RunRecord record : runRecordList) {
                totalDistance = totalDistance + record.getDistanceTotal();
            }
            return totalDistance;
        }
        return 0;
    }

    @Override
    public List<RunRecord> selectUserDistanceByUserIdAndTime(List<Long> userIdList, long startTime, long endTime) {
        RunRecordExample runRecordExample = new RunRecordExample();
        runRecordExample.createCriteria().andUserIdIn(userIdList).andStartTimeGreaterThanOrEqualTo(startTime).andStartTimeLessThan(endTime);
        return runRecordMapper.selectByExample(runRecordExample);
    }


}
